package com.acme.bankapp.service;

import com.acme.bankapp.domain.*;
import com.acme.bankapp.exceptions.QueueIsClosedException;

public class EmailService implements Runnable {
	private boolean isClosed = false;
	private Queue<Email> queue = new QueueImpl<Email>();

	private static EmailService instance = new EmailService();

	public EmailService() {
		Thread thread = new Thread(this);
		thread.start();
	}

	public static EmailService getEmailService() {
		return instance;
	}

	public void close() {
		isClosed = true;
		queue.close();
	}

	public void sendNotificationEmail(Email email) {
		try {
			queue.addEmail(email);
		} catch (QueueIsClosedException e) {
			System.out.println("EmailService is closed");
		}
	}

	public void run() {
		while (!isClosed) {
			Email email = queue.getEmail();
			if (email != null) {
				System.out.println(email);
			}
		}
	}
}
